﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Runda.CMS.Bll;
using System.Data;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class QueryAccessoryStockByPersonal :BasePage
    {

        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "CompanyInstrumentCode";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlVendor.BindDDDW(new CommonBll().dddw("vendor"), "vendor", "vendor");
                ddlVendor.Items.Add(new ListItem("--不限--", ""));
                ddlVendor.SelectedValue = "";
                ddlAccessoryType.SelectedValue = "0";
                txtUserName.Text = base.CurrentUserEntity.UserName;
                Calculate();
                BindGrid();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Calculate();
            BindGrid();
        }

        protected void gridList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            //if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            //{
            //    LinkButton lnkbtnView = e.Item.FindControl("lnkbtnView") as LinkButton;
            //    DataRowView dv = e.Item.DataItem as DataRowView;
            //    lnkbtnView.Attributes.Add("onclick", "openMyFullWin('UserPerformanceView.aspx?UserID=" + dv["UserID"].ToString() + "', 'viewperformanceview');return false;");
            //}
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void Calculate()
        {
            string WhereClause;

            WhereClause = getWhereClause();

            StockBll bll = new StockBll();
            DataTable dt = bll.CalcAccessoryStockByPersonal(WhereClause);

            if (dt != null && dt.Rows.Count > 0)
            {
                hfcount.Value = string.Format("{0:N}", dt.Rows[0]["countSum"]);
                hfquantitySum.Value = string.Format("{0:N}", dt.Rows[0]["quantitySum"]);
            }
            else
            {
                hfcount.Value = "0";
                hfquantitySum.Value = "0";
            }
        }

        private void BindGrid()
        {
            string WhereClause;

            WhereClause = getWhereClause();
            StockBll bll = new StockBll();
            int RecordCount = 0;
            DataTable dt = bll.QueryAccessoryStockByPersonal(WhereClause, SortString, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);
            gridList.DataSource = dt;
            gridList.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();
            if (txtAccessoryName.Text != "")
            {
                sb.Append(" and (FactoryInstrumentCode like '%").Append(txtAccessoryName.Text).Append("%' or CompanyInstrumentCode like  '%").Append(txtAccessoryName.Text).Append("%')");
            }

            if (txtUserName.Text != "")
            {
                sb.Append(" and UserName like '%").Append(txtUserName.Text).Append("%'");
            }

            if (ddlVendor.SelectedValue != "")
            {
                sb.Append(" and Vendor  = '").Append(ddlVendor.SelectedValue.ToString()).Append("'");
            }
            if (System.Convert.ToInt16(ddlAccessoryType.SelectedValue) > 0)
            {
                sb.Append(" and AccessoryType=" + ddlAccessoryType.SelectedValue.ToString());
            }
            return sb.ToString();
        }

        protected void gridList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            Calculate();
            BindGrid();

        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = getWhereClause();

            StockBll bll = new StockBll();

            DataTable dt = bll.ExportAccessoryStockByPersonal(Whereclause, " u.UserName asc ");

            Export.Export2Excel(dt, "个人配件库存", Page.Response);

        }

    }
}
